package com.bigknow.appstore.web.framework.interceptor;

import com.bigknow.appstore.web.framework.util.AuthenticateUserUtil;
import com.bigknow.appstore.web.base.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by TwelveFoldEr on 2016/3/23.
 */
public class LogHandlerInterceptor extends HandlerInterceptorAdapter {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) throws Exception {
        String host = request.getRemoteHost();
        String uri = request.getRequestURI();
        User user = AuthenticateUserUtil.getUser();
        this.logger.debug("IP[{}]User account[{}].", new String[]{host, user.getAccount()});
        this.logger.debug("IP[{}]Request uri [{}].", host, uri);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response,
                           Object handler, ModelAndView modelAndView) throws Exception {
//        System.out.println("===========LogHandlerInterceptor postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
                                Object handler, Exception ex) throws Exception {
//        System.out.println("===========LogHandlerInterceptor afterCompletion");
    }
}
